package cn.javabb.admin.controller.sys;

import cn.javabb.common.model.ResponseModel;
import cn.javabb.common.utils.ListUtils;
import cn.javabb.core.base.BaseController;
import cn.javabb.core.entity.sys.LinkDO;
import cn.javabb.core.service.sys.LinkService;
import cn.javabb.log.action.DeleteAction;
import cn.javabb.log.action.StateAction;
import cn.javabb.log.annotation.Log;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/admin/link")
public class LinkAdminController extends BaseController {
    @Autowired
    private LinkService linkService;
    /**
     * 菜单列表页面
     * @author QINB
     * @param model
     * @return
     */
    @GetMapping("/list")
    public String toListPage(Model model) {
        return "admin/system/link";
    }

    /**
     * 查询角色列表
     */
    @ResponseBody
    @GetMapping("/")
    public Object list() {
        String page = getPage();
        String size = getSize();
        String linkName = getParamValue("keyword");
        LinkDO link = new LinkDO();
        if(StringUtils.isNotBlank(linkName)){
            link.setName(linkName);
        }
        Page linkPage = linkService.queryPageList(Integer.parseInt(page), Integer.parseInt(size), link);
        return ResponseModel.table(linkPage);
    }

    /**
     * 更新
     * @param link
     * @return
     */
    @ResponseBody
    @PostMapping("/")
    public Object saveOrUpdate(LinkDO link){
        int n = 0;
        if(null == link.getId()){
            if(null == link.getState()){link.setState(1);}
            n = linkService.insertSelective(link);
        }else{
            n = linkService.updateSelective(link);
        }
        if(n>0){
            return ResponseModel.Success("保存成功");
        }
        return ResponseModel.Failure("保存失败");

    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @ResponseBody
    @DeleteMapping("/{ids}")
    @Log(name = "友情链接删除",action = DeleteAction.class)
    public Object delete(@PathVariable String ids){
        if(StringUtils.isBlank(ids)){
            return ResponseModel.Failure("参数错误");
        }
        List<String> idList = ListUtils.stringToListString(ids);
        int num = linkService.deleteByIds(LinkDO.class, "id", idList);
        if(num>0){
            return ResponseModel.Success("删除成功");
        }
        return ResponseModel.Failure("删除失败");
    }

    /**
     * 更新状态
     * @param ids
     * @param state
     * @return
     */
    @ResponseBody
    @PostMapping("/updateState")
    @Log(name="友情链接状态",action = StateAction.class)
    public Object updateState(String ids,Integer state){
        if(linkService.updateState(ids,state)){
            return ResponseModel.Success("状态更新成功");
        }
        return ResponseModel.Failure("状态更新失败");
    }
}
